package com.carlinfo.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.mysql.jdbc.Driver;

public class jdbc
{
	public static void main(String[] args)
	{
		System.out.println("====");
		String  url = "jdbc:mysql://localhost:3306/mydata?useSSL=false";
		String user = "root";
		String password = "123456";
		String sql = "select * from dynasty;";
		Connection connection = null;
		Statement st = null;
		ResultSet rs = null;
		try
		{
			Driver driver = new Driver();
			System.out.println(driver);
			Object obj =Class.forName("com.mysql.jdbc.Driver").newInstance();
			System.out.println(obj);
			connection = DriverManager.getConnection(url, user, password);
			st = connection.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next())
			{
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String captital = rs.getString(3);
				short age = rs.getShort("age");
				Date st1 = rs.getDate("st");
				Date createTime = rs.getTime("createTime");
				Date updateTime = rs.getTimestamp("updateTime");
				System.out.println(id + "\t" + name + "\t" + captital + "\t"
						+ age + "\t" + st1.toLocaleString() + "\t"
						+ createTime.toLocaleString() + "\t" + updateTime.toLocaleString());
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		} catch (InstantiationException e)
		{
			e.printStackTrace();
		} catch (IllegalAccessException e)
		{
			e.printStackTrace();
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				if(rs != null) 
				{
					rs.close();
					rs = null;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(st != null)
				{
					st.close();
					st = null;
				}	
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(connection != null)
				{
					connection.close();
					connection = null;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}
